package nio.zerocopy;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * @author chenyuan
 * @date 2020/11/5 9:44
 */
public class Server {
	public static void main(String[] args) throws IOException {
		ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
		serverSocketChannel.socket().bind(new InetSocketAddress(7001));

		// 创建 Buffer
		ByteBuffer buffer = ByteBuffer.allocate(4096);

		while (true) {
			SocketChannel socketChannel = serverSocketChannel.accept();

			int readBytes = 0;
			while (readBytes != -1) {
				try {
					readBytes = socketChannel.read(buffer);
				} catch (Exception e) {
					break;
				}
				// 倒带 ： Position = 0，Mark 作废
				buffer.rewind();
			}
		}
	}
}
